<template>
    <div id="login">
        <div class="login">
            <i class="iconfont icon-gitee"></i>
            <el-button @click="login">Gitee授权登录</el-button>
        </div>
    </div>
</template>
<script>
    import request from "@/lib/request";
    export
        default {
            data() {
                return {
                }
            },
            created() {
                let code = location.search.replace("?code=", "");
                if (code) {
                    this.getAccessToken(code);
                }
            },
            methods: {
                getAccessToken(code) {
                    let that = this;
                    request({
                        url: 'https://gitee.com/oauth/token',
                        method: "POST",
                        data: {
                            grant_type: "authorization_code",
                            code: code,
                            client_id: 'da71d6cb9572a49029c98b191410f426b4bc031a5b70bf542a8e3ea4fa5296ef',//4f91adec967e800e07f8f591d114515b219e62f22c7cb0eed60c1489b989dcd2
                            // redirect_uri: location.href,
                            redirect_uri: "https://gitee.info/login",
                            client_secret: "1ed1404ba1e44bced8ab6ce679dfacd11bf4c9ad008d24ed6c12967f57c596cf",//e52cfcadaccb4e722b387b66cc672fa6561cfb5f5efd7fb35cd28754f477bb2a
                        },
                        noToken: true,
                        success(res) {
                            localStorage.setItem("access_token", res.access_token);
                            request({
                                url: "user",
                                success(res) {
                                    localStorage.setItem("userInfo", JSON.stringify(res));
                                    location.replace("/login");
                                }
                            });
                        }
                    });
                },
                login() {
                    let that = this;
                    let url = 'https://gitee.com/oauth/authorize?client_id=da71d6cb9572a49029c98b191410f426b4bc031a5b70bf542a8e3ea4fa5296ef&redirect_uri=' + encodeURIComponent("https://gitee.info/login") + '&response_type=code';
                    let newWindow = null;
                    if (self.frameElement && self.frameElement.tagName == "IFRAME") {
                        console.log("新窗口打开please");
                        return;
                    } else {
                        newWindow = window.open(url, "", "height=680,width=1080, menubar=no,titlebar=no,scrollbar=no,toolbar=no, status=no,location=no");
                    }
                    that.timer = setInterval(function () {
                        if (newWindow.location.href == location.href) {
                            clearInterval(self.timer);
                            that.$router.push('/');
                            newWindow.close();
                        }
                    }, 100);
                }
            }
        }
</script>
<style>
    #login {
        display: flex;
        align-items: center;
        justify-content: center;
        position: fixed;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
    }

    .login {
        text-align: center;
        display: flex;
        flex-direction: column;
    }

    .login .iconfont {
        font-size: 88px;
        margin-bottom: 20px;
    }

    .login .el-button {
        display: inline;
        margin-bottom: 50px;
    }
</style>